<?php


namespace app\admin\validate;


use app\common\model\Users;
use app\common\model\Wallet;

class AddUserWallet extends BaseValidate
{
    protected $rule = [
        'mobile'                    =>  'require|regex:mobile|checkUser',
        'wid'                       =>  'require|gt:0|checkWallet',
        'money'                     =>  'require|checkMoney',
    ];
    protected $message = [
        'mobile.require'            => '请输入手机号码',
        'mobile.regex'              => '手机号码格式有误',
        'wid.require'               => '请选择钱包类型',
        'wid.gt'                    => '请选择钱包类型',
        'money.require'             => '请输入变动金额',
    ];
    //添加验证
    public function sceneAdd(){
        return $this->only(['mobile','wid','money']);
    }
    //添加验证
    public function sceneEdit(){
        return $this->only(['money']);
    }
    //检查用户是否存在
    protected function checkUser($value, $rule, $data)
    {
        $user=Users::where('mobile',$value)->find();
        if (empty($user)){
            return '会员不存在';
        }
        if ($user['is_lock']==1){
            return '会员状态异常';
        }
        return true;
    }
    //检查钱包是否存在
    protected function checkWallet($value, $rule, $data)
    {
        $wallet=Wallet::find($data['wid']);
        if (empty($wallet)){
            return "钱包不存在";
        }
        return true;
    }
    //检查金额
    protected function checkMoney($value, $rule, $data)
    {
        if ($value==0){
            return "变动金额不能为0";
        }
        return true;
    }
}
